package com.vislardo.app.notebook.view;

import cn.hutool.core.util.StrUtil;
import com.vislardo.app.notebook.model.NoteBook;
import com.vislardo.app.notebook.model.NoteInfo;
import com.vislardo.app.notebook.model.NotePage;
import com.vislardo.app.notebook.utils.NoteFileManager;


import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MainView extends JFrame {

    private NoteInfo info;

    private NoteBook note;
    private JTextArea editArea;
    private JTextPane readPanel;

    JButton saveButton ;
    JButton exportButton;

    JButton prePage ;
    JButton nextPage ;
    JButton expButton;
    public MainView(NoteInfo info) {
        this.info = info;



        setTitle("日记本");
        setSize(1366,768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);


        // 使用分割面板布局
        JSplitPane splitPane  = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setDividerLocation(0.5);
        splitPane.setResizeWeight(0.5);

        //左侧为最新一页编辑区
        JSplitPane leftPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        leftPanel.setDividerLocation(30);
        leftPanel.setResizeWeight(0.0);

        JPanel left_button_panel = new JPanel();
        saveButton = new JButton("保存");
        exportButton = new JButton("导出");

        left_button_panel.add(saveButton);
        left_button_panel.add(exportButton);

        leftPanel.setTopComponent(left_button_panel);
        //初始化文本区
        editArea = new JTextArea();
        editArea.setEditable(true);

        leftPanel.setBottomComponent(editArea);


        //右侧为浏览区
        JSplitPane rightPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        rightPanel.setDividerLocation(30);
        rightPanel.setResizeWeight(0.0);

        //右边为浏览区

        JPanel rb_panel = new JPanel();
        prePage = new JButton("前一页");
        nextPage = new JButton("后一页");
        expButton = new JButton("导出");

        rb_panel.add(prePage);
        rb_panel.add(expButton);
        rb_panel.add(nextPage);

        rightPanel.setTopComponent(rb_panel);

        readPanel = new JTextPane();
        readPanel.setEditable(false);
        rightPanel.setBottomComponent(readPanel);


        splitPane.setLeftComponent(leftPanel);
        splitPane.setRightComponent(rightPanel);

        add(splitPane);

        //加载图书
        loadBook();
        setupButton();
        setVisible(true);

    }

    private void loadBook() {
        if(StrUtil.isEmpty(info.getNoteKey())){
            this.note = NoteBook.startNewNote(info.getNoteName());
            return;
        }

        this.note = NoteFileManager.getInstance().loadEncrtyptBook(info);

        readPanel.setText(note.getLastPage().getContent());
    }

    private void setupButton(){

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = editArea.getText();
                NotePage newPage = NotePage.getNewPage(note.getLastPage());
                newPage.setContent(text);
                note.addPage(newPage);
                info = NoteFileManager.getInstance().appendPageToEncrtyptBook(info,newPage);
                editArea.setText("");
                readPanel.setText(note.getLastPage().getContent());
            }
        });

        prePage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(note.getCurrentPage()==1){
                    JOptionPane.showMessageDialog(null,"已经是第一页");
                    return;
                }
                readPanel.setText(note.turnPage(note.getCurrentPage()-1).getContent());
            }
        });

        nextPage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(note.isLastPage()){
                    JOptionPane.showMessageDialog(null,"已经是最后一页");
                    return;
                }
                readPanel.setText(note.turnPage(note.getCurrentPage()+1).getContent());
            }
        });
    }


}
